/*
Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

/**
 * @file Image plugin
 */

areaImgdialogCommand = function( dialogName )
{
	this.dialogName = dialogName;
};

areaImgdialogCommand.prototype =
{
	exec : function(editor)
	{
		var element = editor.getSelection().getSelectedElement();
		if (element == null || !element.is( 'img' )) {
		  alert("请选择图片");
		  return;
		}
		
		function trimPx(pxNum) {
			if (pxNum == null) {
				return "";
			}
			
			var idx = pxNum.toLowerCase().indexOf("px");
			if (idx == -1) {
				return pxNum;
			} else {
				return Number(pxNum.substring(0, idx));
			}
		}
		
		var myimg = new Image();
		var myurl = element.getAttribute("src");
		myimg.src = myurl;

		var mtwidth = trimPx(element.getStyle("width"));
		var mtheight = trimPx(element.getStyle("height"));
			
		if (mtwidth == "") {
			mtwidth = trimPx(element.getAttribute("width"));
			if (mtwidth == "") {
				mtwidth = myimg.width;
			}
		}
		if (mtheight == "") {
			mtheight = trimPx(element.getAttribute("height"));
			if (mtheight == "") {
				mtheight = myimg.height;
			}
		}
		
		mtwidth = Number(mtwidth);
		mtheight = Number(mtheight);
		if (mtwidth < 100 && mtheight < 100) {
			alert("热区图片过小，使用长宽100px以上图片");
			return;
		}
		
		myimg = null;
		
		editor.openDialog(this.dialogName);
	},
	canUndo: false,
	editorFocus : CKEDITOR.env.ie || CKEDITOR.env.webkit,
	startDisabled : true
};

CKEDITOR.plugins.add( 'areaimage',
{
	init : function( editor )
	{
		var pluginName = 'areaimage';
		// Register the dialog.
		CKEDITOR.dialog.add( pluginName, this.path + 'dialogs/areaimage.js' );

		// Register the command.
		editor.addCommand( pluginName, new areaImgdialogCommand( pluginName ) );
		editor.ui.addButton(name,{ 
                label:'图片热区', 
                icon: this.path + 'timg.jpg', 
                command:name 
            }); 
	  if ( editor.addMenuItems )
		{
			editor.addMenuItems(
				{
					areaimage :
					{
						//label : editor.lang.areaimage.areaimagename,//在lang/zh-cn.js中配这个areaimage:{areaimagename	: '图片热区'},
						label : '图片热区',
						command : pluginName,
						group : 'areaimage'
					}
					
				});
		 }

		if ( editor.contextMenu )
		{
			editor.contextMenu.addListener( function( element, selection )
				{
				if (!element || !element.is( 'img' ) || element.getAttribute( '_cke_realelement' ))
						return null;

					return {
					 areaimage : CKEDITOR.TRISTATE_OFF
					  };
				});
		}

		// Register the toolbar button.
		editor.ui.addButton( 'AreaImage',
			{
				//label : editor.lang.common.image,
				label : "热区",
				command : pluginName
				//icon:　this.path　+　'images/areaimage.png'
			});
			
	  editor.on( 'selectionChange', function( evt )
			{
				/*
				 * Despite our initial hope, document.queryCommandEnabled() does not work
				 * for this in Firefox. So we must detect the state by element paths.
				 */
				var command = editor.getCommand( 'areaimage' );
				var	element = evt.data.element;
				if ( element && element.is( 'img' ) ){
						command.setState( CKEDITOR.TRISTATE_OFF );
				}
				else{
						command.setState( CKEDITOR.TRISTATE_DISABLED );
				}
			} );
	}
} );